<template>
	<view class="yb-radio-group yb-flex" :id="customIdSync" :class="customClassSync" :style="rootStyle">
		<slot></slot>
		<view class="mask yb-flex" v-if="disabled" @touchmove.stop.prevent></view>
	</view>
</template>

<script>
	import ComponentMixin from '../../js_sdk/componentMixin.js'
	import Util from '../../js_sdk/util.js'
	export default {
		name: 'YbRadioGroup',
		mixins: [ComponentMixin],
		props: {
			value: {
				type: [String,Number,Boolean],
				default: ''
			},
			disabled: {
				type: Boolean,
				default: false
			}
		},
		computed: {
			rootStyle () {
				return this.filterStyle(
					{
						opacity: this.disabled ? 0.5 : 1
					}
				) + this.customStyleSync
			}
		},
		data () {
			return {
				valueSync: ''
			}
		},
		mounted() {
			this.valueSync = this.value
			this.deepChildValue(this.valueSync)
		},
		methods: {
			onChange (value) {
				this.valueSync = value
				this.deepChildValue(value)
				this.$emit('change', value)
				this.$emit('input', value)
			},
			deepChildValue (value) {
				let children = Util.getChildrens('YbRadio', this)
				if ( Util.typeof(children) == 'Array' ) {
					children.forEach(child => {
						if ( child.$options.name == 'YbRadio' ) {
							child.setValue(value)
						}
					})
				}
			}
		},
		watch: {
			value (newVal) {
				this.valueSync = newVal
				this.deepChildValue(newVal)
			}
		}
	}
</script>

<style scoped>
	.yb-radio-group {
		position: relative;
	}
	.yb-radio-group .mask {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
	}
</style>